<template>
  <div class="login">
    <div class="mask" @click="$store.commit('modifyShowLogin', false)"></div>
    <div class="login-box">
      <div class="close">
        <img src="../../assets/images/login/userArrow.png" alt="" />
      </div>
      <ul class="login-type flex">
        <li :class="{ active: currentIndex === 0 }" @click="currentIndex = 0">
          手机号码登录
        </li>
        <li :class="{ active: currentIndex === 1 }" @click="weixinClickFn()">
          微信扫码登录
        </li>
      </ul>
      <div class="phone-login" v-show="currentIndex === 0">
        <div class="from-item">
          <input type="text" placeholder="输入手机号" v-model="phone" />
        </div>
        <slide-verify
          :l="60"
          :r="10"
          :w="395"
          :h="135"
          @success="onSuccess"
          @fail="onFail"
          @refresh="onRefresh"
          :style="{ width: '100%' }"
          class="slide-box"
          ref="slideBlock"
          :slider-text="msg"
        ></slide-verify>
        <div class="from-item"></div>
        <div class="from-item">
          <input type="text" placeholder="输入验证码" v-model.trim="code" />
          <span class="btn" @click="getVeriryCode"
            >获取验证码<span v-show="time !== 60">{{ time }}</span></span
          >
        </div>
        <div class="from-item">
          <div class="btn big" @click="phoneLonin">登录</div>
        </div>
      </div>
      <div class="wx-login" v-show="currentIndex === 1">
        <div id="weixin"></div>
      </div>
    </div>
  </div>
</template>

<script>
import { validateTelephone } from "../../utils";
import { toast } from "../../utils/toast.js";
import { request } from "../../http/request.js";
import { getVerifyApi, phoneLoginApi } from "../../http/api";

export default {
  data() {
    return {
      currentIndex: 0,
      msg: "向右滑动",
      time: 60,
      phone: "",
      code: "",
      isSlideVerfiy: false,
    };
  },
  methods: {
    weixinClickFn() {
      this.currentIndex = 1;
      const _this = this;
      new WxLogin({
        id: "weixin",
        appid: "wx67cfaf9e3ad31a0d", // 这个appid要填死
        scope: "snsapi_login",
        // 扫码成功后重定向的接口
        redirect_uri: "https://sc.wolfcode.cn/cms/wechatUsers/shop/PC",
        // state填写编码后的url
        state: encodeURIComponent(
          window.btoa(process.env.VUE_APP_STATE_URL +'/#'+ _this.$route.path)
        ),
        // 调用样式文件
        href: "",
      });
    },
    async phoneLonin() {
      // 验证手机号
      if (!validateTelephone(this.phone))
        return toast("请先填手机号", "dander");
      // 验证是否已经拖动验证成功
      if (!this.isSlideVerfiy) return toast("请先拖动验证", "dander");
      if (this.code === "") return toast("请输入验证码", "dander");

      const res = await phoneLoginApi({ code: this.code, phone: this.phone });
      if (res.code === 0) {
        localStorage.setItem("x-auth-token", res["x-auth-token"]);
        // 提示
        toast(res.message, "sussess");
        // 把login隐藏
        this.$store.commit("modifyShowLogin", false);
      }
    },
    getVeriryCode() {
      // 验证手机号
      if (!validateTelephone(this.phone))
        return toast("请先填手机号", "dander");
      // 验证是否已经拖动验证成功
      if (!this.isSlideVerfiy) return toast("请先拖动验证", "dander");
      // 如果已经点过了，不给点
      if (this.time != 60)
        return toast("验证码已经发送，请勿重复点击", "dander");
      // 修改时间
      this.time--;
      const timer = setInterval(() => {
        this.time--;
        if (this.time == 0) {
          this.time = 60;
          clearInterval(timer);
        }
      }, 1000);
      getVerifyApi(this.phone).then((res) => {
        if (res.code === 0) {
          toast("验证码已经成功发送", "success");
        }
      });
      // 发请求
      request.post("/sendSMS", `phone=${this.phone}`).then((res) => {
        console.log(res);
      });
    },
    // 拼图成功
    onSuccess(times) {
      let ms = (times / 1000).toFixed(1);
      this.msg = "login success, 耗时 " + ms + "s";
      this.isSlideVerfiy = true;
    },
    // 拼图失败
    onFail() {
      this.onRefresh(); // 重新刷新拼图
    },
    // 拼图刷新
    onRefresh() {
      this.msg = "再试一次";
    },
    // 点击登录按钮
    submitFn(formName) {
      if (this.msg == "再试一次" || this.msg == "向右滑动") {
        console.log("请滑动拼图");
      } else {
        console.log("开始登录");
      }
    },
  },
};
</script>

<style lang="less" scoped>
.login {
  background-color: rgba(0, 0, 0, 0.5);
  position: fixed;
  width: 100%;
  height: 100%;
  top: 0;
  left: 0;
  .mask {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
  }
  .login-box {
    box-sizing: border-box;
    width: 555px;
    height: 423px;
    background-image: url("../../assets/images/login/loginBg.png");
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    padding: 40px 80px;
    .close {
      position: absolute;
      top: 20px;
      right: 60px;
      cursor: pointer;
    }
    .login-type {
      justify-content: center;
      li {
        padding: 0 10px;
        cursor: pointer;
        color: #ccc;
        &.active {
          color: #333;
        }
        &:nth-child(2) {
          border-left: 1px solid #ccc;
        }
      }
    }
    .phone-login {
      /deep/.slide-box {
        width: 100%;
        position: relative;
        box-sizing: border-box;
        canvas {
          position: absolute;
          left: 0;
          top: -140px;
          display: none;
          width: 100%;
          box-sizing: border-box;
        }
        .slide-verify-block {
          width: 85px;
          height: 136px;
        }
        .slide-verify-refresh-icon {
          top: -140px;
          display: none;
        }
        &:hover {
          canvas {
            display: block;
          }
          .slide-verify-refresh-icon {
            display: block;
          }
        }
      }
      padding-top: 20px;
      .from-item {
        margin-bottom: 20px;
        display: flex;
        input {
          box-sizing: border-box;
          padding: 0 10px;
          height: 50px;
          border: 1px solid #ccc;
          flex: 1;
        }
        .btn {
          cursor: pointer;
          color: #fff;
          width: 120px;
          height: 50px;
          text-align: center;
          line-height: 50px;
          background-color: #0a328e;
          margin-left: 10px;
          &.big {
            width: 100%;
            margin: 0;
          }
        }
      }
    }
  }
}
</style>